package com.net.oil.product;

import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.product.adapter.ProductClassAdapter;
import com.net.oil.product.bean.ProductBean;
import com.net.oil.product.contract.ProductClassContract;
import com.net.oil.product.presenter.ProductClassPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ProductClassActivity extends BaseActivity<ProductClassPresenter> implements ProductClassContract.View {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.product_recycler_view)
    RecyclerView productRecyclerView;

    private ProductClassAdapter adapter;
    private int parentId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_class);
        ButterKnife.bind(this);
        presenter = new ProductClassPresenter(this);
        initView();
        initData();
    }

    private void initData() {

        parentId = getIntent().getIntExtra(Constant.INTENT_PRODUCT_CLASS_ID, -1);
        if (parentId != -1){
            showProgressDialog();
            presenter.getProductClass(parentId);
        }
    }

    private void initView() {
        toolbarTitle.setText("产品分类");

        adapter = new ProductClassAdapter(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        productRecyclerView.setLayoutManager(manager);
        productRecyclerView.addItemDecoration(new DividerItemDecoration(this, manager.getOrientation()));
        productRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new ProductClassAdapter.OnItemClickListener() {
            @Override
            public void onClick(int id) {
                showProgressDialog();
                parentId = id;
                presenter.getProductClass(id);
            }
        });
    }

    @OnClick(R.id.toolbar_go_back)
    public void onViewClicked() {
        finish();
    }

    /**
     * 获取产品分类成功
     * @param productBean  分类数据
     */
    @Override
    public void getProductClassSuccess(ProductBean productBean) {
        dismissProgressDialog();
        if (productBean != null){
            if (productBean.getCategoryList() != null && productBean.getCategoryList().size() > 0){
                adapter.updateData(productBean.getCategoryList());
            }else {
                if (productBean.getProductList() != null){
                    Intent intent = new Intent(this, ProductListActivity.class);
//                    Bundle bundle = new Bundle();
//                    bundle.putSerializable(Constant.INTENT_PRODUCT_LIST_KEY, productBean.getProductList());
//                    intent.putExtras(bundle);
                    intent.putExtra(Constant.INTENT_PRODUCT_CLASS_ID, parentId);
                    startActivity(intent);
                    finish();
                }
            }
        }
    }

    /**
     * 获取产品分类失败
     */
    @Override
    public void getProductClassFailed() {
        dismissProgressDialog();
    }
}
